<template>
  <div class="top">
    <div>
      <i class="fa-solid fa-sun"></i>
      <router-link to="/" class="router-link"><p>新中地天气</p></router-link>
    </div>
    <span class="city">{{ ip.city }}</span>
    <span>实时天气：{{ weatherData.weather }} {{ weatherData.temperature }}℃</span>
    <span>{{ weatherData.winddirection }}风{{ weatherData.windpower }}级</span>
    <div class="add">
      <i class="fa-solid fa-circle-info" @click = "isActive = true"></i>
      <i class="fa-solid fa-plus" @click = "handleAdd" v-if="isShow" ></i>
    </div>
  </div>
</template>

<script setup>
import { RouterLink } from 'vue-router'
import { onMounted,ref } from 'vue'
import { storeToRefs } from 'pinia'
import { useWeatherStore } from '@/strores/useWeatherStore'
const weather = useWeatherStore()
const { getIpWeather } = weather
const { weatherData, ip,searchWeatherData,isActive,isShow } = storeToRefs(weather)
const handleAdd = () => {
  isShow.value = false
  const localStorageData = localStorage.getItem('data')
  const data = ref([])
  if (localStorageData) {
    data.value = JSON.parse(localStorageData)
  }
  data.value.push({id:searchWeatherData.value.adcode,city:searchWeatherData.value.city,temperature:searchWeatherData.value.temperature})
  localStorage.setItem('data', JSON.stringify(data.value));
  // console.log(JSON.parse(localStorage.getItem('data')))
}

onMounted(() => {
  getIpWeather()
})
</script>

<style lang="scss" scoped>
.top {
  height: 80px;
  box-shadow: 0 8px 6px rgba(0, 0, 0, 0.1);
  display: flex;
  padding-top: 1.5rem;
  padding-bottom: 1.5rem;
  align-items: center;
  margin-right: auto;
  margin-left: auto;
  padding-right: 10rem;
  padding-left: 10rem;
  gap: 1rem;
  :first-child {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    i {
      font-size: 1.5rem;
      line-height: 2rem;
      font-weight: 900;
    }
    .router-link {
      color: inherit;
      text-decoration: none;
      p {
        font-size: 1.5rem;
        line-height: 2rem;
      }
    }
  }
  a {
    font-size: 1.5rem;
    line-height: 2rem;
  }
  span {
    font-size: 0.875rem;
    line-height: 1.25rem;
  }
  .city {
    font-size: 1.25rem;
    line-height: 1.75rem;
  }
  .add {
    flex: 1 1 0%;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 0.75rem;
    i {
      font-size: 1.25rem;
      line-height: 1.75rem;
      font-weight: 900;
      cursor: pointer;
    }
  }
}
</style>
